package com.jinzecloud.reportapproval.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinzecloud.reportapproval.bean.DataStaging;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.TableEnums;
import com.jinzecloud.reportapproval.service.DataStagingService;
import com.jinzecloud.reportapproval.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sampleChecklist")
@RequiredArgsConstructor
public class SampleChecklistController {

    private final DataStagingService dataStagingService;

    @PostMapping("/save")
    public R save(@RequestBody JSONObject object) {
        dataStagingService.save(new DataStaging().setData(object.toJSONString()).setTableId(TableEnums.SAMPLE_SHEET.getTableName()).setExternalId(object.getString("projectNumber")));
        return R.ok();
    }

    @PostMapping("/list")
    public R list(@RequestBody DataStagingParam param) {
        return R.ok().setData(dataStagingService.page(new Page<>(param.getPageNo(), param.getPageSize()), new LambdaQueryWrapper<DataStaging>().eq(StrUtil.isNotEmpty(param.getSearchKey()), DataStaging::getExternalId, param.getSearchKey())));
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        dataStagingService.removeById(id);
        return R.ok();
    }


}
